package com.angel.web.health;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.angel.core.util.JsonUtil;
import com.angel.core.util.Pager;
import com.angel.health.BuzMedicineRecord;
import com.angel.health.ln.MedicineRecordList;
import com.angel.provider.health.BuzMedicineRecordProvider;
import com.github.pagehelper.StringUtil;

/** 
* @author 作者姓名  cheng
* @version 
* 创建时间：2016年9月26日 上午9:03:40 
* 类说明  用药记录
*/
@Controller
@RequestMapping("medicineRecord")
public class BuzMedicineRecordController {
	@Autowired
	private BuzMedicineRecordProvider bmrProvider;

	/**
	 * 查看用药记录
	 * @param request
	 * @param medicineRecord
	 * @param pager
	 * @return
	 */
	@RequestMapping("index")
	public String index(HttpServletRequest request,BuzMedicineRecord medicineRecord,Pager pager){
		medicineRecord.setEnable(1);
		Pager bean = bmrProvider.findAll(medicineRecord,pager);
		request.setAttribute("pager", bean);
		request.setAttribute("medicineRecord", medicineRecord);
		return "pomr/mar_card";
	}
	
	/**
	 * 批量删除
	 * 
	 * @param ids
	 * @return
	 */
	@ResponseBody
	@RequestMapping("del")
	public String del(String ids) {
		int delAll = bmrProvider.delAll(ids);
		if (delAll > 0) {
			return JsonUtil.toJson(delAll);
		} else {
			return JsonUtil.toJson(0);
		}
	}
	
	/**
	 * 跳转添加页
	 * @return
	 */
	@RequestMapping("addPage")
	public String addPage(){
		return "pomr/mar_card_add";
	}
	
	/**
	 * 添加用药记录
	 * @param medicineRecord
	 * @return
	 */
	@RequestMapping("save")
	public String save(HttpServletRequest request,MedicineRecordList mrList){
		List<BuzMedicineRecord> medicineRecordList= new ArrayList<BuzMedicineRecord>();
		String by = request.getSession().getAttribute("user").toString();
		String customerId = request.getParameter("customerId");
		for(BuzMedicineRecord br : mrList.getMrList()){
			if(StringUtil.isEmpty(br.getMedicineName())){
				continue;
			}
			br.setCustomerId(Integer.parseInt(customerId));
			br.setCreateBy(by);
			br.setUpdateBy(by);
			medicineRecordList.add(br);
		}
		bmrProvider.insertAll(medicineRecordList);
		return "redirect:/medicineRecord/index";
	}
	
	/**
	 * 查看用户用药记录
	 * @param request
	 * @param medicineRecord
	 * @return
	 */
	@RequestMapping("/look")
	public String look(HttpServletRequest request,BuzMedicineRecord medicineRecord){
//		medicineRecord.setCustomerId(customerId);
		medicineRecord.setEnable(1);
		List<Map<String, Object>> list = bmrProvider.findAll(medicineRecord);
		request.setAttribute("list", list);
		return "pomr/mar_card_look";
	}
	
	/**
	 * 去往编辑页面
	 * @return
	 */
	@RequestMapping("editPage")
	public String editPage(HttpServletRequest request,BuzMedicineRecord medicineRecord){
		medicineRecord = bmrProvider.findById(medicineRecord);
		request.setAttribute("medicineRecord", medicineRecord);
		return "pomr/mar_card_edit";
	}
	
	@RequestMapping("edit")
	public String edit(BuzMedicineRecord medicineRecord){
		int updateById = bmrProvider.updateById(medicineRecord);
		return "redirect:/medicineRecord/index";
	}
}
